package com.yayo.mangement.scheduled;

import com.alibaba.fastjson.JSON;
import com.yayo.mangement.utils.HttpUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class LotteryTicket {

    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/mangement";
    private static final String USER = "root";
    private static final String PASSWORD = "root";

    public static void main(String[] args) {
        // 彩票网站api，分页查询接口
        String api = "https://www.cwl.gov.cn/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=&issueEnd=&dayStart=&dayEnd=&pageNo=1&pageSize=1766&week=&systemType=PC";
        // 调用接口，获取数据
        String data = HttpUtils.sendGet(api);
        System.out.println(data);
        Result result = JSON.parseObject(data, Result.class);
        List<LotteryTicketData> resultList = result.getResult();

        String sql = "INSERT INTO `lottery_ticket`(`code`, `name`, `date`, `poolmoney`, `sales`, `week`, `red`, `blue_one`, `blue_two`, `content`, `prizegrades`) VALUES (?,?,?,?,?,?,?,?,?,?,?)";

        try (Connection connection = DriverManager.getConnection(JDBC_URL, USER, PASSWORD);
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {

            for (LotteryTicketData ticket : resultList) {
                preparedStatement.setString(1, ticket.getCode());
                preparedStatement.setString(2, ticket.getName());
                preparedStatement.setString(3, ticket.getDate());
                preparedStatement.setLong(4, ticket.getPoolmoney());
                preparedStatement.setLong(5, ticket.getSales());
                preparedStatement.setString(6, ticket.getWeek());
                preparedStatement.setString(7, ticket.getRed());
                preparedStatement.setString(8, ticket.getBlue());
                preparedStatement.setString(9, ticket.getBlue2());
                preparedStatement.setString(10, ticket.getContent());
                preparedStatement.setString(11, ticket.getPrizegrades());

                preparedStatement.addBatch();
            }

            preparedStatement.executeBatch();
            System.out.println("批量插入成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static class LotteryTicketData {
        private String code;
        private String name;
        private String date;
        private Long poolmoney;
        private Long sales;
        private String week;
        private String red;
        private String blue;
        private String blue2;
        private String content;
        private String prizegrades;

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public Long getPoolmoney() {
            return poolmoney;
        }

        public void setPoolmoney(Long poolmoney) {
            this.poolmoney = poolmoney;
        }

        public Long getSales() {
            return sales;
        }

        public void setSales(Long sales) {
            this.sales = sales;
        }

        public String getWeek() {
            return week;
        }

        public void setWeek(String week) {
            this.week = week;
        }

        public String getRed() {
            return red;
        }

        public void setRed(String red) {
            this.red = red;
        }

        public String getBlue() {
            return blue;
        }

        public void setBlue(String blue) {
            this.blue = blue;
        }

        public String getBlue2() {
            return blue2;
        }

        public void setBlue2(String blue2) {
            this.blue2 = blue2;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public String getPrizegrades() {
            return prizegrades;
        }

        public void setPrizegrades(String prizegrades) {
            this.prizegrades = prizegrades;
        }
    }

    public static class Result {
        private String message;
        private int state;
        private Integer pageNo;
        private Integer pageNum;
        private Integer pageSize;
        private Integer total;
        private List<LotteryTicketData> result;

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public int getState() {
            return state;
        }

        public void setState(int state) {
            this.state = state;
        }

        public Integer getPageNo() {
            return pageNo;
        }

        public void setPageNo(Integer pageNo) {
            this.pageNo = pageNo;
        }

        public Integer getPageNum() {
            return pageNum;
        }

        public void setPageNum(Integer pageNum) {
            this.pageNum = pageNum;
        }

        public Integer getPageSize() {
            return pageSize;
        }

        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }

        public Integer getTotal() {
            return total;
        }

        public void setTotal(Integer total) {
            this.total = total;
        }

        public List<LotteryTicketData> getResult() {
            return result;
        }

        public void setResult(List<LotteryTicketData> result) {
            this.result = result;
        }
    }
}
